/*
 * System : Ticketing
 * Package: com.ql2.ticketing.util
 * Class: TicketingUtil
 *
 * © QL2 Software 2011
 */
package main.java.org.dss.lm.admin.util;

import main.java.org.dss.lm.admin.common.ITSConstants;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Calendar;
import java.util.Random;
import java.util.Collections;
import java.util.Collection;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class LocMgmtUtil {
    /**
     * Default format for display if the user doesn't have any preferences.
     */
    private static String dFDefaultStr = "MM/dd/yyyy";

    private static String tFDefaultStr = "hh:mm a";

    private static SimpleDateFormat dFDefault = new SimpleDateFormat(dFDefaultStr);

    /**
     * Miscellaneous date formats.
     */
    private static SimpleDateFormat dFEEEdMMMyyyyhhmma =
            new SimpleDateFormat("EEE, d MMM yyyy hh:mm a");

    private static SimpleDateFormat dFMMMyyyy = new SimpleDateFormat("MMM yyyy");

    private static SimpleDateFormat dFMMMd = new SimpleDateFormat("MMM d");

    private static SimpleDateFormat dFyyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

    private static SimpleDateFormat dFMMMW = new SimpleDateFormat("MMM W");

    private static SimpleDateFormat dFEEEdMMMyyyy = new SimpleDateFormat("EEE, d MMM yyyy");

    private static SimpleDateFormat dFEEEMMd = new SimpleDateFormat("EEE, MM/d");

    private static SimpleDateFormat dFhhmma = new SimpleDateFormat("hh:mm a");

    private static SimpleDateFormat dFMMddyyyyhhmmss = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");

    private static SimpleDateFormat dFDDmmyyyyhhmmss = new SimpleDateFormat("dd/MM/yyyy hh:mm a");

    private static SimpleDateFormat dFyyyyMMddhhmmss = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

    private static SimpleDateFormat dFyyyyMMddhhmmsshypen =
            new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    private static SimpleDateFormat dFkmm = new SimpleDateFormat("k:mm");

    private static SimpleDateFormat dFHmma = new SimpleDateFormat("H:mm a");

    private static SimpleDateFormat dFddMMyyyy = new SimpleDateFormat("dd-MM-yyyy");

    private static SimpleDateFormat dFddMMyyyyhhmmss = new SimpleDateFormat("dd-MM-yyyy hh:mm");

    private static SimpleDateFormat uniqueNumber = new SimpleDateFormat("ddMMyyyyhhmmss");

    private static SimpleDateFormat dFddMMyyyyHHmmss = new SimpleDateFormat("dd-MM-yyyy HH:mm");

    /**
     * Decimal number formats.
     */
    private static String largeDoublePrecision = ".0000";

    private static String doublePrecision = ".00";


    /**
     * This methods parses string date into the date object.
     *
     * @param dateStr the String object.
     * @return Date value parse of the given String date.
     * @throws java.text.ParseException during date parsing.
     */
    public static Date parseDefault(
            String dateStr
    ) throws ParseException {
        return
                dFDefault.parse(dateStr);
    }

    /**
     * This methods parses string date into the date object.
     *
     * @param dateStr the String object.
     * @return Date value parse of the given String date.
     * @throws java.text.ParseException during date parsing.
     */
    public static Date parseEEEdMMMyyyyhhmma(
            String dateStr
    ) throws ParseException {
        return
                dFEEEdMMMyyyyhhmma.parse(dateStr);
    }

    /**
     * This methods parses string date into the date object.
     *
     * @param dateStr the String object.
     * @return Date value parse of the given String date.
     * @throws java.text.ParseException during date parsing.
     */
    public static Date parseyyyyMMdd(
            String dateStr
    ) throws ParseException {
        return
                dFyyyyMMdd.parse(dateStr);
    }

    /**
     * This methods parses string date into the date object.
     *
     * @param dateStr the String object.
     * @return Date value parse of the given String date.
     * @throws java.text.ParseException during date parsing.
     */
    public static Date parseEEEdMMMyyyy(
            String dateStr
    ) throws ParseException {
        return
                dFEEEdMMMyyyy.parse(dateStr);
    }

    /**
     * This methods parses string date into the date object.
     *
     * @param dateStr the String object.
     * @return Date value parse of the given String date.
     * @throws java.text.ParseException during date parsing.
     */
    public static Date parseyyyyMMddhhmmss(
            String dateStr
    ) throws ParseException {
        return
                dFyyyyMMddhhmmss.parse(dateStr);
    }

    /**
     * This method checks whether the give list contains only zeros.
     *
     * @param list the List<Float> object.
     * @return boolean indicating whether the list contains only zeros or not.
     */
    public static boolean containsOnlyZeros(List<Float> list) {
        for (Float item : list) {
            if (item != new Float(0)) {
                return false;
            }
        }
        return list.isEmpty() ? false : true;
    }

    /**
     * Checks whether given string is null or blank.
     *
     * @param str String to be checked for null or blank.
     * @return Returns true if the give string is null or blank.
     */
    public static boolean isNullOrBlank(String str) {
        return (str == null) || (str.trim().length() == 0);
    }

    /**
     * Checks whether given string is valid login string.
     *
     * @param str String to be checked for valid login.
     * @return Returns true if the give string is valid login field.
     */
    public static boolean isValidLoginField(String str) {
        return (str.length() < 5 || str.length() > 20);
    }

    /**
     * Checks whether given string is valid task string.
     *
     * @param str String to be checked for valid task.
     * @return Returns true if the give string is valid tAsk field.
     */
    public static boolean isValidTaskField(String str) {
        return (str.length() < 4 || str.length() > 25);
    }

    /**
     * Checks whether given string is null or blank or wrong spelt.
     *
     * @param str String to be checked for null or blank or misspell.
     * @return Returns true if the give string is null or blank.
     */
    public static boolean isNullOrBlankOrSpeltSo(String str) {
        return ((str == null) || (str.trim().length() == 0) || (str.indexOf("null") >= 0));
    }

    /**
     * Checks whether given Object is null or blank.
     *
     * @param obj object to be checked for null.
     * @return Returns true if the give object is not null or blank.
     */
    public static boolean isNotNull(Object obj) {
        return (obj != null);
    }

    /**
     * Caps the first letter of the String.
     *
     * @param s String to be first capitalized.
     * @return String capitalizing the first letter of the given string.
     */
    public static String capFirstLetter(String s) {
        String first, rest;
        first = s.substring(0, 1);
        rest = s.substring(1);
        first = first.toUpperCase();
        rest = rest.toLowerCase();
        s = first + rest;
        return s;
    }

    /**
     * Gets the default date format String  ("MM/dd/YYYY").
     *
     * @return the String value of the dFDefaultStr.
     */
    public static String getDefaultFormatStr() {
        return dFDefaultStr;
    }

    /**
     * Gets the Formated Time stamp in given format (Default MM/dd/yyyy) .
     *
     * @param d     the Date Object.
     * @param dFStr String value.
     * @return String value of the Formated Time stamp in given format.
     */
    public static String getFormatedDate(Date d, String dFStr) {
        if (d == null) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(isNullOrBlank(dFStr) ? dFDefaultStr : dFStr);
        return sdf.format(d);
    }

    /**
     * Gets the Formated Time stamp in given format (Default MM/dd/yyyy).
     *
     * @param d the Date Object.
     * @return String value of the Formated Time stamp in default format.
     */
    public static String getFormatedInputDate(Date d) {
        if (d == null) {
            return null;
        }
        return dFyyyyMMddhhmmss.format(d);
    }

    /**
     * Gets the Formated Time stamp in k:mm format.
     *
     * @param d the Date object.
     * @return String value of the Formated Date in k:mm format.
     */
    public static String getkmmFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFkmm.format(d);
    }

    /**
     * Gets the date and time in MM/dd/yyyy HH:mm:ss format.
     *
     * @param d the Date Object.
     * @return String value of the formatted Date in MM/dd/yyyy HH:mm:ss format.
     */
    public static String getdFddMMyyyy(Date d) {
        if (d == null) {
            return null;
        }
        return dFddMMyyyy.format(d);
    }

    /**
     * Gets the Formatted Time stamp in MMM YYYY format.
     *
     * @param d the Date object.
     * @return String value of the formatted TimeStamp in MMM YYYY format.
     */
    public static String getDefaultFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFDefault.format(d);
    }

    /**
     * Gets the Formatted Time stamp in EEEdMMMyyyyhhmma format.
     *
     * @param d the Date object.
     * @return String value of the formatted TimeStamp in EEEdMMMyyyyhhmma format.
     */
    public static String getEEEdMMMyyyyhhmmaFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFEEEdMMMyyyyhhmma.format(d);
    }

    /**
     * Gets the Formatted Time stamp in Hmma format.
     *
     * @param d the Date object.
     * @return String value of the formatted TimeStamp in Hmma format.
     */
    public static String getHmmaFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFHmma.format(d);
    }

    /**
     * Gets the Formatted Time stamp in MMMyyyy format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MMMyyyy format.
     */
    public static String getMMMyyyyFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFMMMyyyy.format(d);
    }

    /**
     * Gets the Formatted Time stamp in MMMd format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MMMd format.
     */
    public static String getMMMdFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFMMMd.format(d);
    }

    /**
     * Gets the Formatted Time stamp in yyyyMMdd format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in yyyyMMdd format.
     */
    public static String getyyyyMMddFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFyyyyMMdd.format(d);
    }

    /**
     * Gets the Formatted Time stamp in dFddMMyyyyhhmmss format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in dFddMMyyyyhhmmss format.
     */
    public static String getdFddMMyyyyhhmmss(Date d) {
        if (d == null) {
            return null;
        }
        return dFddMMyyyyhhmmss.format(d);
    }

    /**
     * Gets the Formated Time stamp in dd-MM-YYYY HH:mm format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in dd-MM-YYYY HH:mm format.
     */
    public static String getdFddMMyyyyHHmmss(Date d) {
        if (d == null) {
            return null;
        }
        return dFddMMyyyyHHmmss.format(d);
    }

    /**
     * Gets the formatted Time stamp in MMM W format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MMM W format.
     */
    public static String getMMMWFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFMMMW.format(d);
    }

    /**
     * Gets the date and time in MM/dd/yyyy HH:mm:ss format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MM/dd/yyyy HH:mm:ss format.
     */
    public static String getdFMMddyyyyhhmmss(Date d) {
        if (d == null) {
            return null;
        }
        return dFMMddyyyyhhmmss.format(d);
    }

    /**
     * Gets the date and time in MM/dd/yyyy HH:mm:ss format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MM/dd/yyyy HH:mm:ss format.
     */
    public static String getYYYYMMDDhhmmss(Date d) {
        if (d == null) {
            return null;
        }
        return dFyyyyMMddhhmmss.format(d);
    }

    /**
     * Gets the date and time in MM/dd/yyyy HH:mm:ss format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MM/dd/yyyy HH:mm:ss format.
     */
    public static String getYYYYMMDDhhmmssHypen(Date d) {
        if (d == null) {
            return null;
        }
        return dFyyyyMMddhhmmsshypen.format(d);
    }

    /**
     * Gets the date and time in MM/dd/yyyy HH:mm:ss format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in MM/dd/yyyy HH:mm:ss format.
     */
    public static String getDFDDmmyyyyhhmmss(Date d) {
        if (d == null) {
            return null;
        }
        return dFDDmmyyyyhhmmss.format(d);
    }

    /**
     * Gets the Formated Time stamp in EEE, d MMM yyyy format.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in EEE, d MMM yyyy format.
     */
    public static String getEEEdMMMyyyyFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFEEEdMMMyyyy.format(d);
    }

    /**
     * Gets the Formated Time stamp in  format EEE, MM/d.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in EEE, MM/d format.
     */
    public static String getEEEMMdFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFEEEMMd.format(d);
    }

    /**
     * Gets the Formated Time stamp in  format hh:mm.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in hh:mm format.
     */
    public static String gethhmmaFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFhhmma.format(d);
    }

    /**
     * Gets the Formated Time stamp in  format yyyy-MM-dd hh:mm:ss.
     *
     * @param d the Date object.
     * @return String value of the formatted Time stamp in yyyy-MM-dd hh:mm:ss format.
     */
    public static String getyyyyMMddhhmmssFormat(Date d) {
        if (d == null) {
            return null;
        }
        return dFyyyyMMddhhmmss.format(d);
    }

    /**
     * Gets the Formated Time stamp in  SimpleDateFormat.
     *
     * @param date   the Date object.
     * @param format the SimpleDateFormat object.
     * @return String value of the formatted Time stamp in SimpleDateFormat format.
     */
    public static String formatDate(Date date, SimpleDateFormat format) {
        String str = "";

        if (date != null && format != null) {
            str = format.format(date);
        }

        return str;
    }

    /**
     * Gets the Formated Time stamp in  format yyyy-MM-dd hh:mm:ss.
     *
     * @param d the String Object.
     * @return the String value of the fomatted Time stamp in the yyyy-MM-dd hh:mm:ss format.
     */
    public static String getyyyyMMddhhmmssFormat(String d) {
        if (d == null) {
            return null;
        }
        return dFyyyyMMddhhmmss.format(Timestamp.valueOf(d));
    }

    /**
     * This method formats the date with the given format.
     *
     * @param format the String object.
     * @param obDate the Date Object.
     * @return string value of the formatted date string.
     */
    public static String doFormatDate(String format, Date obDate) {
        String strFormat = "";
        //Instantiating the object of date format.
        if (!LocMgmtUtil.isNullOrBlank(format)) {
            SimpleDateFormat obSDF = new SimpleDateFormat(format);

            // if date object is not null.
            if (LocMgmtUtil.isNotNull(obDate)) {
                // format the date with the given format.
                strFormat = obSDF.format(obDate);
            }
        }
        // return the formatted date.
        return strFormat;
    }

    /**
     * Splits date and return the date parts.
     *
     * @param date the Date object.
     * @return String[] value of the splatted date part.
     */
    public static String[] splitDate(Date date) {

        String strDate = getdFMMddyyyyhhmmss(date);

        String[] arrParts = strDate.split("\\s");

        return arrParts;
    }

    /**
     * Splits date and return the date parts.
     *
     * @param date the Date object and time String object.
     * @return Date value of the splatted date part.
     */
    public static Date joinDate(Date date, String time) {

        String strDate = getYYYYMMDDhhmmssHypen(date);

        String[] arrParts = strDate.split("\\s");
        // assign time to part 2.
        arrParts[1] = time;

        StringBuilder dateStr = new StringBuilder(arrParts[0]);
        dateStr.append(" ");
        dateStr.append(time);

        return getDateInStandard(dateStr.toString());
    }

    /**
     * For a given Month, date, year format seperated by /, returns the date without the time.
     *
     * @param date the String object.
     * @return Date value of the splatted date part excluding the time part.
     */
    public static Date getDate(String date) {
        if (date == null) {
            return null;
        }

        String[] splits = date.split("/");
        Calendar cal = Calendar.getInstance();
        Date retDate = null;
        try {
            if (splits.length >= 3) {
                cal.set
                        (
                                Integer.parseInt(splits[2]),
                                Integer.parseInt(splits[0]) - 1,
                                Integer.parseInt(splits[1])
                        );
            }
            retDate = cal.getTime();
        } catch (NumberFormatException ne) {
        }
        return retDate;
    }

    /**
     * For a given Timestamp object return the String value of the Timestamp in SimpleDate Format.
     *
     * @param timestamp the Timestamp object.
     * @return String value given timestamp object in SimpleDateFormat.
     */
    public static String getDate(Timestamp timestamp) {
        SimpleDateFormat format = new SimpleDateFormat(dFDefaultStr);
        return format.format(timestamp);
    }

    /**
     * For a given Timestamp object return the String value of the Timestamp in SimpleDate Format.
     *
     * @param date the String object, hour int value, int minutes.
     * @return Calendar value of the given date, hour and minute.
     */
    public static Calendar getCalendar(String date, int hour, int minutes) {
        String[] splits = date.split("/");
        Calendar cal = Calendar.getInstance();

        if (splits.length == 3) {

            cal.set
                    (
                            Integer.parseInt(splits[2]),
                            Integer.parseInt(splits[0]) - 1,
                            Integer.parseInt(splits[1]),
                            hour,
                            minutes
                    );
        }
        return cal;
    }

    /**
     * This method compares the two date. Condition: If Date 1 is less than Date 2 then, 1 will be
     * returned else if Date 2 is less than Date 1 then, -1 will be returned else both are equal, 0
     * will be returned.
     *
     * @param date1 the Date object.
     * @param date2 the Date object.
     * @return int value representing the date1 is less than or greater than or equal to the date2.
     */
    public static int compareDate(Date date1, Date date2) {
        int result;

        return result = date1.compareTo(date2);
    }

    /**
     * It formats the number in double precision format.
     *
     * @param number double value.
     * @return String value of the passed double number which is formatted number.
     */
    public static String numDoublePrecision(double number) {

        // Instantiate an object of number format
        NumberFormat obFormat = new DecimalFormat(doublePrecision);

        // returns the number in a specified format
        return obFormat.format(number);
    }

    /**
     * It formats the number in double precision format.
     *
     * @param number double value.
     * @return String value of the passed double number which is formatted number.
     */
    public static String largeDoublePrecision(double number) {

        // Instantiate an object of number format.
        NumberFormat obFormat = new DecimalFormat(largeDoublePrecision);

        // returns the number in a specified format.
        return obFormat.format(number);
    }

    /**
     * It generates the unique number from the date object.
     *
     * @return long unique number.
     */
    public static String getUniqueNumber() {
        Date obCur = new Date();

        return uniqueNumber.format(obCur);
    }

    /**
     * It validates the given String format date to be a valid date in the MM/dd/yyyy format.
     *
     * @param date the String object in the format of MM/dd/yyyy.
     * @return boolean value representing the given date is a valid date in the MM/dd/yyyy format.
     * @throws Exception during parsing the String date int the date object.
     */
    public static boolean isValidDate(String date) {
        try {
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            sdf.setLenient(false);
            sdf.parse(date);
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    /**
     * It checks whether the given input is zero or null.
     *
     * @param id the Long value.
     * @return boolean value representing the input is null or zero.
     */
    public static boolean isNullOrZero(Long id) {
        Long zero = 0L;
        if (id == null || zero.equals(id)) {
            return true;
        }
        return false;
    }

    /**
     * It checks whether the given input is zero or null.
     *
     * @param id the Double value.
     * @return boolean value representing the input is null or zero.
     */
    public static boolean isNullOrZero(Double id) {
        Double zero = 0D;
        if (id == null || zero.equals(id)) {
            return true;
        }
        return false;
    }

    /**
     * It checks whether the given input is zero or null.
     *
     * @param id the Float value.
     * @return boolean value representing the input is null or zero.
     */
    public static boolean isNullOrZero(Float id) {
        Float zero = 0F;
        if (id == null || zero.equals(id)) {
            return true;
        }
        return false;
    }

    /**
     * It checks whether the given input is zero or null or negative value.
     *
     * @param id the Long value.
     * @return boolean value representing the input is null or zero.
     */
    public static boolean isNullOrZeroOrNegative(Long id) {
        return isNullOrZero(id) || id.intValue() < 0;
    }

    /**
     * It checks whether the given input is zero or null value.
     *
     * @param id the Integer value.
     * @return boolean value representing the input is null or zero.
     */
    public static boolean isNullOrZero(Integer id) {
        Integer zero = 0;
        if (id == null || zero.equals(id)) {
            return true;
        }
        return false;
    }

    /**
     * It checks whether the given input is zero or null value.
     *
     * @param id the Integer value.
     * @return boolean value representing the input is null or is not null.
     */
    public static boolean isNull(Integer id) {
        if (id == null) {
            return true;
        }
        return false;
    }

    /**
     * It checks the given String. If the given String is null empty string is returned else actual
     * string is return by removing the space.
     *
     * @param str the String object.
     * @return String value against the given input.
     */
    public static String chkString(String str) {
        if (str == null) {
            return "";
        }
        return str.trim();
    }

    /**
     * It checks the validity of two dates such that the first date comes before second dare.
     *
     * @param fromDate the String object.
     * @param toDate   the String Object.
     * @return boolean value representing the validity of two dates.
     * @throws Exception during parsing the String date into the Date object.
     */
    public static boolean isDatesValid(String fromDate, String toDate) throws Exception {
        return getDateInStandardFormat(fromDate).before(getDateInStandardFormat(toDate));
    }

    /**
     * It checks the validity of two dates such that the first date comes before second dare.
     *
     * @param fromDate the String object.
     * @param toDate   the String Object.
     * @return boolean value representing the validity of two dates.
     * @throws Exception during parsing the String date into the Date object.
     */
    public static boolean isDateValid(String fromDate, String toDate) throws Exception {
        return getDateStandardFormat(fromDate).before(getDateStandardFormat(toDate));
    }

    /**
     * It checks the validity of two dates such that the first date comes before second dare.
     *
     * @param fromDate the String object.
     * @param toDate   the String Object.
     * @return boolean value representing the validity of two dates.
     * @throws Exception during parsing the String date into the Date object.
     */
    public static boolean isValidDates(String fromDate, String toDate) throws Exception {
        return getDateInStandard(fromDate).before(getDateInStandard(toDate));
    }

    /**
     * It gets the date in Standard format against the given input.
     *
     * @param str the String object.
     * @return Date value representing the date in Simple Date format.
     */
    public static Date getDateInStandardFormat(String str) {
        Date obDate = new Date();
        try {
            obDate = new SimpleDateFormat("yyyy/MM/dd").parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obDate;
    }

    /**
     * It gets the date in Standard format against the given input.
     *
     * @param str the String object.
     * @return Date value representing the date in Simple Date format.
     */
    public static Date getDateInFormat(String str) {
        Date obDate = new Date();
        try {
            obDate = new SimpleDateFormat("MM/dd/yyyy").parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obDate;
    }

    /**
     * It gets the date in Standard format against the given input.
     *
     * @param date the Date object.
     * @return Date value representing the date in Simple Date format.
     */
    public static Date getSimpleDateFormat(Date date) {
        String strDate = getYYYYMMDDhhmmss(date);

        String[] dateParts = strDate.split(" ");

        Date obDate = new Date();

        try {
            obDate = new SimpleDateFormat("yyyy/MM/dd").parse(dateParts[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obDate;
    }

    /**
     * It gets the date in Standard format against the given input.
     *
     * @param str the String object.
     * @return Date value representing the date in Simple Date format.
     * @throws Exception during parsing the String object into date object.
     */
    public static Date getDateStandardFormat(String str) throws Exception {
        return new SimpleDateFormat("yyyy-MM-dd").parse(str);
    }

    /**
     * It gets the date in Standard format against the given input.
     *
     * @param str the String object.
     * @return Date value representing the date in Simple Date format.
     */
    public static Date getDateInStandard(String str) {

        Date obDate = null;

        try {

            obDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(str);
        } catch (ParseException e) {
            //TODO: Auto-generated catch block
            e.printStackTrace();
        }

        return obDate;
    }

    /**
     * It checks whether two given dates are equal or not.
     *
     * @param fromDate the String object.
     * @param toDate   the String object.
     * @return boolean value representing whether given two dates are equal or not.
     * @throws Exception during parsing the string date into the date object.
     */
    public static boolean isDateEqual(String fromDate, String toDate) throws Exception {
        return getYYMMDDFormat(fromDate).equals(getYYMMDDFormat(toDate));
    }

    /**
     * It takes date in String format and returns the date object in the form of yyyy-MM-dd.
     *
     * @param str the String object.
     * @return Date value in yyyy-MM-dd format.
     * @throws Exception during parsing the string date into the date object.
     */
    public static Date getYYMMDDFormat(String str) throws Exception {
        return new SimpleDateFormat("yyyy-MM-dd").parse(str);
    }

    /**
     * It takes an String object and checks whether the given input is numeric or not.
     *
     * @param obj the String object.
     * @return boolean value representing whether the given input is numeric or not.
     */
    public static boolean isNumeric(String obj) {
        return obj.matches("[0-9]*");
    }

    /**
     * It takes an String object and checks whether the given input is alpha-numeric or not.
     *
     * @param obj the String object.
     * @return boolean value representing whether the given input is alpha-numeric or not.
     */
    public static boolean isAlphaNumeric(String obj) {
        return obj.matches("[0-9A-Za-z]*");
    }

    /**
     * It takes an String object and returns the corresponding value. If the given input is null or
     * blank null is returned else Long value is returned.
     *
     * @param id the String object.
     * @return Long value w.r.t given input.
     */
    public static Long getLongValue(String id) {
        if (isNullOrBlank(id)) {
            return null;
        }

        return Long.valueOf(id);
    }

    /**
     * It takes an String object and returns the corresponding value. If the given input is null or
     * blank null is returned else Long value is returned.
     *
     * @param id the String object.
     * @return Integer value w.r.t given input.
     */
    public static Integer getIntValue(String id) {
        if (isNullOrBlank(id)) {
            return null;
        }

        return new Integer(id);
    }

    /**
     * It takes an Long object and returns the corresponding string value. If the given input is
     * null or zero null is returned else String value of the given Long input is returned.
     *
     * @param id the Long object.
     * @return String value w.r.t given input.
     */
    public static String getStringValue(Long id) {
        if (isNullOrZero(id)) {
            return null;
        }

        return Long.toString(id);
    }

    /**
     * It takes a List and checks whether it is empty or not.
     *
     * @param list the List object.
     * @return boolean value representing whether passed input list is empty or not.
     */
    public static boolean isEmptyList(List list) {
        return ((list == null) || list.isEmpty());
    }

    /**
     * It takes a List and checks whether it is empty or not.
     *
     * @param c the Collection object.
     * @return boolean value representing whether passed input collection is empty or not.
     */
    public static boolean isEmptyCollection(Collection c) {
        return ((c == null) || c.isEmpty());
    }

    /**
     * It takes an array and checks whether it is empty or not.
     *
     * @param arr the Object[] object.
     * @return boolean value representing whether passed input Object[] is empty or not.
     */
    public static boolean isEmptyArray(Object[] arr) {
        return (arr == null || arr.length == 0);
    }

    /**
     * It takes an String object and return empty string if the string is null else return string
     * after removing the empty spaces.
     *
     * @param str the String object.
     * @return String value either null or the given input string after removing the white spaces.
     */
    public static String trimString(String str) {
        if (str == null) {
            return "";
        }

        return str.trim();
    }

    /**
     * Generates a Random Password for the given length.
     *
     * @param n the int value.
     * @return String which is randomly generated string.
     */
    public static String getPassword(int n) {
        char[] pw = new char[n];
        int c = 'A';
        int r1 = 0;
        for (int i = 0; i < n; i++) {
            r1 = (int) (Math.random() * 3);
            switch (r1) {
                case 0:
                    c = '0' + (int) (Math.random() * 10);
                    break;
                case 1:
                    c = 'a' + (int) (Math.random() * 26);
                    break;
                case 2:
                    c = 'A' + (int) (Math.random() * 26);
                    break;
            }
            pw[i] = (char) c;
        }
        return new String(pw);
    }

    /**
     * This method sorts the input List.
     *
     * @param list the List object.
     */
    public static void sort(List list) {
        if (list != null) {
            Collections.sort(list);
        }
    }

    /**
     * Returns a pseudorandom number minimum of five digits.
     *
     * @return int value a pseudo random number.
     */
    public static int generateRandomNumber() {
        Random random = new Random();
        int randomNumber = random.nextInt(2147483647);

        if (randomNumber < 99999) {
            randomNumber += 11111;
        }

        return randomNumber;
    }

    /**
     * This method replace the special character with single quote of the given input String.
     *
     * @param str the String object.
     * @return String after replacing the special character.
     */
    public static String escapeSpecialChars(String str) {
        if (!isNullOrBlank(str)) {
            str = str.replace("'", "\\'");
        }

        return str;
    }

    /**
     * This method identify modification in given two input string.
     *
     * @param oldVal the String object.
     * @param newVal the String object.
     * @return boolean value representing whether two given input strings are modified or not.
     */
    public static boolean isModified(String oldVal, String newVal) {
        boolean retVal;

        if (oldVal == null) {
            oldVal = "";
        }
        if (newVal == null) {
            newVal = "";
        }

        retVal = !oldVal.equals(newVal);

        return retVal;
    }

    /**
     * This method identify modification in given two input string.
     *
     * @param oldVal the Object object.
     * @param newVal the Object object.
     * @return boolean value representing whether two given input Objects are modified or not.
     */
    public static boolean isModified(Object oldVal, Object newVal) {
        boolean retVal;
        if (oldVal == null) {
            if (newVal != null) {
                retVal = true;
            } else {
                retVal = false;
            }
        } else {
            retVal = !oldVal.equals(newVal);
        }

        return retVal;
    }

    /**
     * This method checks the validity of start and end date.
     *
     * @return boolean value representing start date and end date are valid.
     */
    public static boolean isDate1ExistOverDate2(
            Date date1Start,
            Date date1End,
            Date date2Start,
            Date date2End) {
        return date2Start.after(date1Start) && date2End.before(date1End);
    }

    /**
     * Returns true if email valid else return false.
     *
     * @param emailAddress the String object.
     * @return boolean value indicating valid or invalid email.
     */
    public static boolean isEmailAddressValid(String emailAddress) {

        if (emailAddress != null) {
            if (!((emailAddress.contains("..")) || (emailAddress.contains("__")) ||
                    (emailAddress.contains("._")) || (emailAddress.contains("_.")) ||
                    (emailAddress.contains(".@")) || (emailAddress.contains("@.")) ||
                    (emailAddress.contains("_@")))) {
                if ((emailAddress.length() > 4) && (emailAddress.length() <= 128)) {
                    Pattern email_pattern = Pattern
                            .compile("[a-zA-Z][a-zA-Z0-9._]*[@][a-zA-Z0-9.]+[.][a-zA-Z]+[a-zA-Z]");
                    Matcher email_matcher = email_pattern.matcher(emailAddress);
                    boolean email_matches = email_matcher.matches();
                    return email_matches;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Returns true if nickname is valid else return false.
     *
     * @param nickName the String object.
     * @return boolean value indicating valid or invalid nickname.
     */
    public static boolean isValidNickName(String nickName) {
        if (nickName != null) {
            if ((nickName.length() > 4) && (nickName.length() <= 10)) {
                Pattern nickName_pattern = Pattern.compile("[a-zA-Z0-9_-]+");
                Matcher nickName_matcher = nickName_pattern.matcher(nickName);
                boolean nickName_matches = nickName_matcher.matches();

                return nickName_matches;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Returns true if password is valid else return false.
     *
     * @param password the String object.
     * @return boolean value indicating valid or invalid password.
     */
    public static boolean isValidPassword(String password) {
        if (password != null) {
            if ((password.length() > 5) && (password.length() <= 8)) {
                Pattern password_pattern = Pattern.compile("[a-z][a-z0-9]+");
                Matcher password_matcher = password_pattern.matcher(password);
                boolean password_matches = password_matcher.matches();

                if (password.equals("password")) {
                    return false;
                }
                return password_matches;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Returns a random ID against the the given String object.
     *
     * @param nickName the String object.
     * @return String object indicating random id for the nickName.
     */
    public static String getRandomId(String nickName) {

        char[] chars = new char[nickName.length() + 36];
        int i = 0;
        for (i = 0; i < nickName.length(); i++) {
            chars[i] = nickName.charAt(i);
        }
        i = nickName.length();

        for (char c = '0'; c <= '9'; c++) {
            chars[i++] = c;
        }

        for (char c = 'a'; c <= 'z'; c++) {
            chars[i++] = c;
        }

        StringBuffer code = new StringBuffer("");
        for (i = 0; i < 16; i++) {
            char c = chars[(int) (Math.random() * chars.length)];
            code = code.append(c);
        }

        for (int nInnerLoop = 0x00; nInnerLoop < code.length(); nInnerLoop++) {
            int rand = (int) (Math.random() * chars.length);
            if (code.charAt(nInnerLoop) == '_' || code.charAt(nInnerLoop) == '-' ||
                    (Character.isLetter(code.charAt(nInnerLoop)) && nInnerLoop == rand)) {
                code.replace(nInnerLoop, nInnerLoop + 1,
                        String.valueOf((nInnerLoop + code.charAt(nInnerLoop)) % 0x0a));
            }
        }

        return code.toString();
    }

    /**
     * This method takes a String date and returns the Date object of that string.
     *
     * @param date the String object.
     * @return Date object in the  EEE MMM dd HH:mm:ss z yyyy format.
     */
    public static Date getTimeStamp(String date) {
        //Timestamp timestamp = null;
        Date dt = null;
        String dateFormat = "yyyy-MM-dd";
        try {
            dt = parseyyyyMMdd(new SimpleDateFormat(dateFormat)
                    .format(new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(date.toString())));
        } catch (Exception e) {            
        }
        return dt;
    }

    /**
     * This method takes a date object and returns the Date object in yyyy-MM-dd hh:mm:ss format.
     *
     * @param date the Date object.
     * @return Date object in the  yyyy-MM-dd hh:mm:ss format.
     */
    public static Date getDatePickerTimeStamp(Date date) {
        Date currentTime_2 = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            ParsePosition pos = new ParsePosition(0);
            currentTime_2 = formatter.parse(formatter.format(date), pos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentTime_2;
    }

    /**
     * This method takes a date object and returns the Timestamp object.
     *
     * @param date the Date object.
     * @return Timestamp object.
     */
    public static Timestamp getTimeStampByDate(Date date) {
        Timestamp timestamp = null;
        try {
            timestamp = new Timestamp(date.getTime());
        } catch (Exception e) {

            e.printStackTrace();
        }
        return timestamp;
    }

    /**
     * This method two Date objects input and calculate the differences between the input 2 dates.
     *
     * @param date1 the Date object.
     * @param date2 the Date object.
     * @return int value indicating the days of differences in those two dates.
     */
    public static int dateDiff(Date date1, Date date2) {
        Long diff = Math.abs(date1.getTime() - date2.getTime());
        int day = 86400000; // 1000 * 60 * 60 * 24f
        int datediff = (diff.intValue() / day);
        return datediff;
    }

    /**
     * Calculates date difference in years.
     *
     * @param dFrom the Date object and dTo the Date object.
     * @return int indicating the years of difference between the Date object dFrom and dTo.
     */
    public static int dateDiffYears(Date dFrom, Date dTo) {
        float diff = dateDiff(dFrom, dTo);
        // Divide the difference by 365 days.
        Double years = Math.ceil(diff / 365f);

        return years.intValue();
    }

    /**
     * This method determines whether the passed string to the method special characters or not.
     *
     * @param strChars the String object.
     * @return boolean value indication whether special character found in the provided String
     *         object or not.
     */
    public static boolean isSpecialCharProperty(String strChars) {
        // if the value is null return false
        if (LocMgmtUtil.isNullOrBlank(strChars)) {
            return false;
        }

        // Initialize the pattern to identify special characters
        Pattern obPattern = Pattern.compile("[^,a-zA-Z0-9&. \\s _ @-]");

        Matcher obMatch = obPattern.matcher(strChars);

        // If the given string contains any special characters.
        if (obMatch.find()) {
            return true;
        }

        // If there is no special characters it returns false.
        return false;
    }

    /**
     * This method loads the properties email protocol settings file and reads the each property one and add each individual
     * property into mailProtocolDetailsList for mail reading purpose.
     * @return a List of mail Protocol details for the mail reading.
     */
    public static List<String> getMailReaderProtocolDetailsList() {
        List mailProtocolDetailsList = new ArrayList<String>();
        try {

            InputStream is = LocMgmtUtil.class.getClassLoader().getResourceAsStream(ITSConstants.APPMAIL_RESOURCE_PROPERTIES);
            Properties pro = new Properties();
            pro.load(is);

            mailProtocolDetailsList.add(ITSConstants.ZEROTH_INDEX, pro.getProperty(ITSConstants.MAIL_STORE_PROTOCOL));
            mailProtocolDetailsList.add(ITSConstants.FIRST_INDEX, pro.getProperty(ITSConstants.MAIL_IMAPS_HOST));
            mailProtocolDetailsList.add(ITSConstants.SECOND_INDEX, pro.getProperty(ITSConstants.MAIL_USER));
            mailProtocolDetailsList.add(ITSConstants.THIRD_INDEX, pro.getProperty(ITSConstants.MAIL_PASSWORD));
            mailProtocolDetailsList.add(ITSConstants.FOURTH_INDEX, pro.getProperty(ITSConstants.MAIL_READER_FOLDER));
        } catch (IOException e) {            
        }
        return mailProtocolDetailsList;
    }

    /**
     * This method loads the properties email protocol settings file and reads the each property one and add each individual
     * property into mailProtocolDetailsList for mail sending purpose.
     * @return a List of mail Protocol details for the mail sending.
     */
    public static List getMailSenderProtocolDetailsList() {
        List<String> mailProtocolDetailsList = new ArrayList<String>();
        try {

            InputStream is = LocMgmtUtil.class.getClassLoader().getResourceAsStream(ITSConstants.APPMAIL_RESOURCE_PROPERTIES);
            Properties pro = new Properties();
            pro.load(is);

            mailProtocolDetailsList.add(ITSConstants.ZEROTH_INDEX, pro.getProperty(ITSConstants.MAIL_TRANSPORT_PROTOCOL));
            mailProtocolDetailsList.add(ITSConstants.FIRST_INDEX, pro.getProperty(ITSConstants.MAIL_SMTP_HOST));
            mailProtocolDetailsList.add(ITSConstants.SECOND_INDEX, pro.getProperty(ITSConstants.MAIL_SMTP_PORT));
            mailProtocolDetailsList.add(ITSConstants.THIRD_INDEX, pro.getProperty(ITSConstants.MAIL_SMTP_AUTH));
            mailProtocolDetailsList.add(ITSConstants.FOURTH_INDEX, pro.getProperty(ITSConstants.MAIL_SMTP_STARTTLS_ENABLE));
            mailProtocolDetailsList.add(ITSConstants.FIFTH_INDEX, pro.getProperty(ITSConstants.MAIL_USER));
            mailProtocolDetailsList.add(ITSConstants.SIXTH_INDEX, pro.getProperty(ITSConstants.MAIL_PASSWORD));
        } catch (IOException exp) {            
        }
        return mailProtocolDetailsList;
    }


    /**
     * This method loads the properties email protocol settings file and reads the email contents which is to be sent to
     * the mailer then replace the subject line with the ticket title.
     * @return ticketGenerationMailContent String value.
     */
    public static String getTicketGenerationMailContent(String ticketTitle) {
        String ticketGenerationMailContent = null;
        try {

            InputStream is = LocMgmtUtil.class.getClassLoader().getResourceAsStream(ITSConstants.APPMAIL_RESOURCE_PROPERTIES);
            Properties pro = new Properties();
            pro.load(is);
            ticketGenerationMailContent = pro.getProperty(ITSConstants.TICKET_GENERATION_MAIL_CONTENT);
            ticketGenerationMailContent = ticketGenerationMailContent.replace(ITSConstants.MAIL_SUBJECT, ticketTitle);

        } catch (IOException exp) {            
        }

        return ticketGenerationMailContent;
    }

    /**
     * This method loads the properties email protocol settings file and reads the email contents.
     * @return ticketGenerationMailContent String value.
     */
    public static String getInsufficientInputMailContent() {
        String insufficientInputMailContent = null;
        try {

            InputStream is = LocMgmtUtil.class.getClassLoader().getResourceAsStream(ITSConstants.APPMAIL_RESOURCE_PROPERTIES);
            Properties pro = new Properties();
            pro.load(is);
            insufficientInputMailContent = pro.getProperty(ITSConstants.INSUFFICIENT_SUBJECT_MAIL_CONTENT);

        } catch (IOException exp) {            
        }

        return insufficientInputMailContent;
    }

} // End Class TicketingUtil

